// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
// 云函数入口函数
exports.main = async(event, context) => {
  const wxContext = cloud.getWXContext()

  console.log("star-event: ", event)
  var myword = event.word_id
  var myopenid = wxContext.OPENID
  const countResult = await db.collection('star').where({
    word_id: myword,
    user_id: myopenid
  }).count()
  const num = countResult.total
  if (num == 0) {
    await db.collection('star').add({
      data: {
        word_id: myword,
        user_id: myopenid
      },
      success: function(res) {
        // res 是一个对象，其中有 _id 字段标记刚创建的记录的 id
      }
    })
    return {
      msg: 'ok'
    }
  } else
    return {
      msg: 'error'
    }

}